# -*- coding: iso-8859-15 -*-

import main as Main
from Bio import AlignIO
from Bio.Align.Applications import MuscleCommandline
from Bio.Align.Applications import ClustalwCommandline

from globals import *

__author__ = 'Admin'

def howToUse():
    ''' Shows available options
    '''

    print ''
    print '=============================================='
    print '          Wielodopasowanie z pliku:'
    print '1. Przykłady'
    print '2. Wczytaj i wyświetl plik z wielodopasowaniem'
    print '3. Wczytaj plik z sekwencjiami i przeprowadź wielodopasowanie'
    print '4. Powrót do Menu Głównego'
    print '=============================================='

def ShowExamples():
    ''' Shows Examples available in this application
    '''
    print ''
    print '=============================================='
    print '          Przykłady:'
    print '1. Plik bez wielodopasowania - MUSCLE'
    print '2. Plik bez wielodopasowania (duży) - MUSCLE'
    print '3. Plik bez wielodopasowania - ClustalW'
    print '4. Plik bez wielodopasowania (duży) - ClustalW'
    print '5. Plik z wielodopasowaniem'
    print '6. Powrót do Wielodopasowań z pliku'
    print '=============================================='

    option = raw_input('\nWybierz opcję: ')
    if option == str(1):
        Example_WithoutMultipleAlignment_Muscle()
    elif option == str(2):
        Example_WithoutMultipleAlignment2_Mulsce()
    elif option == str(3):
        Example_WithoutMultipleAlignment_ClustalW()
    elif option == str(4):
        Example_WithoutMultipleAlignment2_ClustalW()
    elif option == str(5):
        Example_WithMultipleAlignment()
    elif option == str(6):
        main()
    else:
        print '\nNie ma takiej opcji. Wybierz ponownie.'

    ShowExamples()

def ShowMultipleAlignment(fileWithMultipleAlignment, type='fasta'):
    print ''

    alignment_rows = []
    records = 0
    max_size_of_row = 60
    for alignment in AlignIO.parse(fileWithMultipleAlignment, type):
        print("Alignment length %i" % alignment.get_alignment_length())

        for record in alignment:
            temp_seq = ''
            length = 0
            previous_length = 1
            records += 1

            for letter in record.seq:
                temp_seq += letter
                if letter != '-':
                    length += 1

                if len(temp_seq) == max_size_of_row:
                    alignment_rows.append(("%d %s - %d %s" % (previous_length, temp_seq, length,record.id)))
                if len(temp_seq) > max_size_of_row:
                    temp_seq = ''
                    temp_seq += letter
                    previous_length = length

        for alignment in range(len(alignment_rows)/records):
            for record in range(records):
                print( alignment_rows[alignment + len(alignment_rows)/records * record] )

            print ''

        print("")

def CreateMultipleAlignmentUsingMuscleExe(inputfile,outputfile):
    muscle_cline = MuscleCommandline(muscle_exe, input=inputfile, out=outputfile)
    stdout, stderr = muscle_cline()

def CreateMultipleAlignmentUsingClustalWExe(inputfile):
    clustalw_cline = ClustalwCommandline(clustalw_exe, infile=inputfile)
    stdout, stderr = clustalw_cline()

def Example_WithMultipleAlignment():
    ''' Example - using file where there are sqeuences with Alignments
    '''

    ShowMultipleAlignment('..\data\human_with_multiplealignment.txt')

def Example_WithoutMultipleAlignment_Muscle():
    ''' Example - using file where there are only sequences without alignment
    '''

    output_file = 'temp_output_file.txt'

    CreateMultipleAlignmentUsingMuscleExe('..\data\human_without_multiplealignment.txt', output_file)
    ShowMultipleAlignment(output_file)

def Example_WithoutMultipleAlignment2_Mulsce():
    ''' Example - using file where there are only sequences without alignment
    '''

    output_file = 'temp_output_file.txt'

    CreateMultipleAlignmentUsingMuscleExe('..\data\human_without_multiplealignment2.txt', output_file)
    ShowMultipleAlignment(output_file)

def Example_WithoutMultipleAlignment_ClustalW():
    ''' Example - using file where there are only sequences without alignment
    '''

    output_file = 'temp_output_file.txt'

    CreateMultipleAlignmentUsingClustalWExe('..\data\human_without_multiplealignment.txt')
    ShowMultipleAlignment('..\data\human_without_multiplealignment.aln', "clustal")

def Example_WithoutMultipleAlignment2_ClustalW():
    ''' Example - using file where there are only sequences without alignment
    '''

    output_file = 'temp_output_file.txt'

    CreateMultipleAlignmentUsingClustalWExe('..\data\human_without_multiplealignment2.txt')
    ShowMultipleAlignment('..\data\human_without_multiplealignment2.aln', "clustal")

def ShowMultipleAlignmentFile():
    ''' Reads and prints multiple alignments from file
    '''

    path_to_file_with_multipeAlignment = raw_input('\nPodaj ścieżkę do pliku z wielodopasowaniem: ')

    ShowMultipleAlignment(path_to_file_with_multipeAlignment)

def CreateMultipleAlignmentsFromFile():
    ''' Create and prints multiple alignment form sequences from file
    '''
    output_file = 'temp_output_file.txt'

    path_to_file_without_multipeAlignment = raw_input('\nPodaj ścieżkę do pliku z sekwencjami: ')

    CreateMultipleAlignmentUsingMuscleExe(path_to_file_without_multipeAlignment,output_file )
    ShowMultipleAlignment(output_file)

def main():
    ''' gets option chosen by user
    '''
    howToUse()
    option = raw_input('\nWybierz opcję: ')
    if option == str(1):
        ShowExamples()
    elif option == str(2):
        ShowMultipleAlignmentFile()
    elif option == str(3):
        CreateMultipleAlignmentsFromFile()
    elif option == str(4):
        Main.main()
    else:
        print '\nNie ma takiej opcji. Wybierz ponownie.'
    main()

def Run():
    '''
    '''
    main()

